package com.youi.manage;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import com.youi.exception.BusinessException;
import com.youi.util.CommonInfoUtil;

/**
 * http request 管理工具
 * @author erdong.Chan
 *
 */
public class HttpRequestManage {

	/**
	  *   获取Integer类型数据
	 * @param key
	 * @param request
	 * @return
	 * @throws BusinessException
	 */
	public Integer getInteger(String key,HttpServletRequest request) throws BusinessException{
		String value = request.getParameter(key);
		if(value == null){
			throw new BusinessException("缺少参数：" + key, CommonInfoUtil.PARAMERROR);
		}
		try {
			return Integer.valueOf(value);
		} catch (Exception e) {
			// TODO: handle exception
			throw new BusinessException("参数" + key + "格式不正确", CommonInfoUtil.PARAMERROR);
		}
	}
	
	/**
	  *   尝试获取Integer类型数据	默认返回null
	 * @param key
	 * @param request
	 * @return
	 * @throws BusinessException
	 */
	public Integer optInteger(String key,HttpServletRequest request) throws BusinessException{
		String value = request.getParameter(key);
		if(value == null||value.equals("")){
			return null;
		}
		try {
			return Integer.valueOf(value);
		} catch (Exception e) {
			// TODO: handle exception
			throw new BusinessException("参数" + key + "格式不正确", CommonInfoUtil.PARAMERROR);
		}
	}
	
	/**
	  *   尝试获取Integer类型数据
	 * @param key
	 * @param request
	 * @param deaultValue  默认值
	 * @return
	 * @throws BusinessException
	 */
	public Integer optInteger(String key,HttpServletRequest request,Integer deaultValue) throws BusinessException{
		String value = request.getParameter(key);
		if(value == null||value.equals("")){
			return deaultValue;
		}
		try {
			return Integer.valueOf(value);
		} catch (Exception e) {
			// TODO: handle exception
			throw new BusinessException("参数" + key + "格式不正确", CommonInfoUtil.PARAMERROR);
		}
	}
	
	/**
	  *   获取Long类型数据
	 * @param key
	 * @param request
	 * @return
	 * @throws BusinessException
	 */
	public Long getLong(String key,HttpServletRequest request) throws BusinessException{
		String value = request.getParameter(key);
		if(value == null||value.length()==0){
			throw new BusinessException("缺少参数：" + key, CommonInfoUtil.PARAMERROR);
		}
		try {
			return Long.valueOf(value);
		} catch (Exception e) {
			// TODO: handle exception
			throw new BusinessException("参数" + key + "格式不正确", CommonInfoUtil.PARAMERROR);
		}
	}
	
	/**
	  *   尝试获取Long类型数据	默认返回null
	 * @param key
	 * @param request
	 * @return
	 * @throws BusinessException
	 */
	public Long optLong(String key,HttpServletRequest request) throws BusinessException{
		String value = request.getParameter(key);
		if(value == null||value.equals("")){
			return null;
		}
		try {
			return Long.valueOf(value);
		} catch (Exception e) {
			// TODO: handle exception
			throw new BusinessException("参数" + key + "格式不正确", CommonInfoUtil.PARAMERROR);
		}
	}
	
	/**
	  *   尝试获取Long类型数据
	 * @param key
	 * @param request
	 * @param deaultValue  默认值
	 * @return
	 * @throws BusinessException
	 */
	public Long optLong(String key,HttpServletRequest request,Long deaultValue) throws BusinessException{
		String value = request.getParameter(key);
		if(value == null||value.equals("")){
			return deaultValue;
		}
		try {
			return Long.valueOf(value);
		} catch (Exception e) {
			// TODO: handle exception
			throw new BusinessException("参数" + key + "格式不正确", CommonInfoUtil.PARAMERROR);
		}
	}
	
	/**
	  *   获取Float类型数据
	 * @param key
	 * @param request
	 * @return
	 * @throws BusinessException
	 */
	public Float getFloat(String key,HttpServletRequest request) throws BusinessException{
		String value = request.getParameter(key);
		if(value == null){
			throw new BusinessException("缺少参数：" + key, CommonInfoUtil.PARAMERROR);
		}
		try {
			return Float.valueOf(value);
		} catch (Exception e) {
			// TODO: handle exception
			throw new BusinessException("参数" + key + "格式不正确", CommonInfoUtil.PARAMERROR);
		}
	}
	
	/**
	  *  尝试获取Float类型数据	  默认返回null
	 * @param key
	 * @param request
	 * @return
	 * @throws BusinessException
	 */
	public Float optFloat(String key,HttpServletRequest request) throws BusinessException{
		String value = request.getParameter(key);
		if(value == null||value.equals("")){
			return null;
		}
		try {
			return Float.valueOf(value);
		} catch (Exception e) {
			// TODO: handle exception
			throw new BusinessException("参数" + key + "格式不正确", CommonInfoUtil.PARAMERROR);
		}
	}
	
	/**
	  *  尝试获取Float类型数据
	 * @param key
	 * @param request
	 * @param deaultValue  默认值
	 * @return
	 * @throws BusinessException
	 */
	public Float optFloat(String key,HttpServletRequest request,Float deaultValue) throws BusinessException{
		String value = request.getParameter(key);
		if(value == null||value.equals("")){
			return deaultValue;
		}
		try {
			return Float.valueOf(value);
		} catch (Exception e) {
			// TODO: handle exception
			throw new BusinessException("参数" + key + "格式不正确", CommonInfoUtil.PARAMERROR);
		}
	}
	
	/**
	  *   获取Double类型数据
	 * @param key
	 * @param request
	 * @return
	 * @throws BusinessException
	 */
	public Double getDouble(String key,HttpServletRequest request) throws BusinessException{
		String value = request.getParameter(key);
		if(value == null){
			throw new BusinessException("缺少参数：" + key, CommonInfoUtil.PARAMERROR);
		}
		try {
			return Double.valueOf(value);
		} catch (Exception e) {
			// TODO: handle exception
			throw new BusinessException("参数" + key + "格式不正确", CommonInfoUtil.PARAMERROR);
		}
	}
	
	/**
	  *  尝试获取Double类型数据	  默认返回null
	 * @param key
	 * @param request
	 * @return
	 * @throws BusinessException
	 */
	public Double optDouble(String key,HttpServletRequest request) throws BusinessException{
		String value = request.getParameter(key);
		if(value == null||value.equals("")){
			return null;
		}
		try {
			return Double.valueOf(value);
		} catch (Exception e) {
			// TODO: handle exception
			throw new BusinessException("参数" + key + "格式不正确", CommonInfoUtil.PARAMERROR);
		}
	}
	
	/**
	  *  尝试获取Double类型数据
	 * @param key
	 * @param request
	 * @param deaultValue  默认值
	 * @return
	 * @throws BusinessException
	 */
	public Double optDouble(String key,HttpServletRequest request,Double deaultValue) throws BusinessException{
		String value = request.getParameter(key);
		if(value == null||value.equals("")){
			return deaultValue;
		}
		try {
			return Double.valueOf(value);
		} catch (Exception e) {
			// TODO: handle exception
			throw new BusinessException("参数" + key + "格式不正确", CommonInfoUtil.PARAMERROR);
		}
	}
	
	/**
	  *   获取Double类型数据
	 * @param key
	 * @param request
	 * @return
	 * @throws BusinessException
	 */
	public String getString(String key,HttpServletRequest request) throws BusinessException{
		String value = request.getParameter(key);
		if(value == null){
			throw new BusinessException("缺少参数：" + key, CommonInfoUtil.PARAMERROR);
		}
		try {
			return value;
		} catch (Exception e) {
			// TODO: handle exception
			throw new BusinessException("参数" + key + "格式不正确", CommonInfoUtil.PARAMERROR);
		}
	}
	
	/**
	  *  尝试获取String类型数据	  默认返回null
	 * @param key
	 * @param request
	 * @return
	 * @throws BusinessException
	 */
	public String optString(String key,HttpServletRequest request) throws BusinessException{
		String value = request.getParameter(key);
		if(value == null||value.equals("")){
			return null;
		}
		try {
			return value;
		} catch (Exception e) {
			// TODO: handle exception
			throw new BusinessException("参数" + key + "格式不正确", CommonInfoUtil.PARAMERROR);
		}
	}
	
	/**
	  *  尝试获取String类型数据
	 * @param key
	 * @param request
	 * @param deaultValue  默认值
	 * @return
	 * @throws BusinessException
	 */
	public String optString(String key,HttpServletRequest request,String deaultValue) throws BusinessException{
		String value = request.getParameter(key);
		if(value == null||value.equals("")){
			return deaultValue;
		}
		try {
			return value;
		} catch (Exception e) {
			// TODO: handle exception
			throw new BusinessException("参数" + key + "格式不正确", CommonInfoUtil.PARAMERROR);
		}
	}
	
	/**
	  *   获取Date类型数据
	 * @param key
	 * @param request
	 * @param format  格式化格式
	 * @return
	 * @throws BusinessException
	 */
	public Date getDate(String key,HttpServletRequest request,String format) throws BusinessException{
		String value = request.getParameter(key);
		if(value == null){
			throw new BusinessException("缺少参数：" + key, CommonInfoUtil.PARAMERROR);
		}
		try {
			SimpleDateFormat simpdf = new SimpleDateFormat(format);
			return simpdf.parse(value);
		} catch (Exception e) {
			// TODO: handle exception
			throw new BusinessException("参数" + key + "格式["+format+"]不正确", CommonInfoUtil.PARAMERROR);
		}
	}
	
	/**
	  *  尝试获取Date类型数据	  默认返回null
	 * @param key
	 * @param request
	 * @param format  格式化格式
	 * @return
	 * @throws BusinessException
	 */
	public Date optDate(String key,HttpServletRequest request,String format) throws BusinessException{
		String value = request.getParameter(key);
		if(value == null||value.equals("")){
			return null;
		}
		try {
			SimpleDateFormat simpdf = new SimpleDateFormat(format);
			return simpdf.parse(value);
		} catch (Exception e) {
			// TODO: handle exception
			throw new BusinessException("参数" + key + "格式["+format+"]不正确", CommonInfoUtil.PARAMERROR);
		}
	}
	
	/**
	  *  尝试获取Date类型数据
	 * @param key
	 * @param request
	 * @param deaultValue  默认值
	 * @param format  格式化格式
	 * @return
	 * @throws BusinessException
	 */
	public Date optDate(String key,HttpServletRequest request,String format,String deaultValue) throws BusinessException{
		String value = request.getParameter(key);
		if(value == null||value.equals("")){
			try {
				SimpleDateFormat simpdf = new SimpleDateFormat(format);
				return simpdf.parse(deaultValue);
			} catch (Exception e) {
				// TODO: handle exception
				throw new BusinessException("参数" + deaultValue + "格式["+format+"]不正确", CommonInfoUtil.PARAMERROR);
			}
		}
		try {
			SimpleDateFormat simpdf = new SimpleDateFormat(format);
			return simpdf.parse(value);
		} catch (Exception e) {
			// TODO: handle exception
			throw new BusinessException("参数" + key + "格式["+format+"]不正确", CommonInfoUtil.PARAMERROR);
		}
	}
	
	/**
	  *  尝试获取Date类型数据
	 * @param key
	 * @param request
	 * @param deaultValue  默认值
	 * @param format  格式化格式
	 * @return
	 * @throws BusinessException
	 */
	public Date optDate(String key,HttpServletRequest request,String format,Date deaultValue) throws BusinessException{
		String value = request.getParameter(key);
		if(value == null||value.equals("")){
			return deaultValue;
		}
		try {
			SimpleDateFormat simpdf = new SimpleDateFormat(format);
			return simpdf.parse(value);
		} catch (Exception e) {
			// TODO: handle exception
			throw new BusinessException("参数" + key + "格式["+format+"]不正确", CommonInfoUtil.PARAMERROR);
		}
	}
}
